<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends CI_Controller {
    public function __construct(){
        parent::__construct();

    }

	public function index()
	{
        $data['message'] = $this->session->flashdata('message');
        $data['title'] = 'Admin Login';
		$this->load->view('admin/header',$data);
		$this->load->view('admin/login',$data);
		$this->load->view('admin/footer',$data);
	}
    public function login()
    {
       $data['title'] = 'Admin Login';
       $user = $this->input->post('userid');
       $pass = $this->input->post('password');
       $this->form_validation->set_rules('userid', 'username', 'required|min_length[5]');
       $this->form_validation->set_rules('password', 'password', 'required|min_length[6]');
       $check = $this->adminmodel->is_valid();
       if($this->form_validation->run()== true){
            if($check){
                $result = $this->adminmodel->GetAlldByUsername($user);
                $infor = array();
                $infor = $result[0];
                $this->session->set_userdata($infor);
                redirect(base_url().'admin/home');
            }else{
                flash_message('message', 'Invalid Username and/or Password.', 2);
                redirect(base_url());
            }
       }else{
           $this->load->view('admin/header',$data);
           $this->load->view('admin/login',$data);
           $this->load->view('admin/footer',$data);
       }
    }

    public function home()
    {
            $data['title'] = 'Admin Home';
            $this->load->view('admin/header',$data);
            $this->load->view('admin/home',$data);
            $this->load->view('admin/footer',$data);

    }
    public function logout()
    {
        $this->session->sess_destroy();
        redirect(base_url());
    }
    public function user()
    {
        $data['title'] = 'Users';
        $data['result'] = $this->adminmodel->GetInforUser();
        $this->load->view('admin/header',$data);
        $this->load->view('admin/user',$data);
        $this->load->view('admin/footer',$data);

    }

    // show add user form
    public function show_add_user()
    {
        $data['title'] = 'Add new user';
        // $data['result'] = $this->adminmodel->GetInforUser();
        $this->load->view('admin/add_user',$data);
    }

    // add new user function
    public function add_user()
    {
        $username = $this->input->post('username');
        $email = $this->input->post('email');
        $passOr = $this->input->post('password');
        $address = $this->input->post('address');
        $select = $this->input->post('select');
        $fullname = $this->input->post('fullname');
        $password = base64_encode($passOr);
        $key = md5(uniqid());
        $arrayName = array(
                            'u_username' => $username,
                            'u_fullname' => $fullname,
                            'u_email' => $email,
                            'u_password' => $password,
                            'u_address' => $address,
                            'u_ugroup_id' => $select,
                            'u_status'=>'Unverify',
                            'u_code'=>$key,
                            'u_dateregister' => date('Y-m-d'),  
                          );
        $this->adminmodel->Add_User($arrayName);
    }


    public function check_user()
    {
              $itest = 'user';
              $username= $this->input->post('username');
              $check = $this->adminmodel->CheckAll($username,$itest);
              echo $check;
    }

    public function check_email()
    {
              $itest = 'email';
              $email= $this->input->post('email');
              $check = $this->adminmodel->CheckAll($email,$itest);
              echo $check;
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */